package com.zhdl.modules.demo.testprocess;

import com.zhdl.common.ytools.callback.CommonCallback;
import com.zhdl.modules.common.warn.service.RunWarnService;
import com.zhdl.modules.process.BaseProcess;
import com.zhdl.modules.process.dto.ProcessBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class TestMainProcess extends BaseProcess {
    private final int needCount = 99;
    private int doneCount = 0;
    @Resource
    TestProcessInit testProcessInit;
    @Resource
    TestBranch1 testBranch1;
    @Resource
    TestBranch2 testBranch2;
    @Resource
    RunWarnService runWarnService;

    @Override
    public void initHardware() {
        statusHolder.setAuto(true);
        testBranch1.statusHolder.setAuto(true);
        testBranch2.statusHolder.setAuto(true);
        initProcessList();
        testBranch1.initHardware();
        testBranch2.initHardware();
        childList.add(testBranch1);
        childList.add(testBranch2);
        statusHolder.setOptInterface(new MainOptImp(childList,this){
            @Override
            public void release() {
                super.release();
                doneCount = 0;
                dynamicTask.stopAll();
            }

            @Override
            public void reset() {
                runWarnService.resetAll();
            }
        });
    }

    @Override
    public void initLocation(CommonCallback<String> complete) {
        super.initLocation(complete);
        complete.statusCallback("ok");
    }

    @Override
    public void maxStepCallback(ProcessBean maxStep) {
        doneCount++;
        if (doneCount < needCount) {
            maxStep.setStep(0);
        } else {
            maxStep.setStep(-1);
        }
    }

    @Override
    public void initProcessList() {
        processBeanList = testProcessInit.getTestMainProcess(s -> {
            testBranch1.statusHolder.start();
        }, s -> {
            testBranch2.statusHolder.start();
            statusHolder.startProcess(statusHolder.process + 1);
        });
    }
}
